$(function(){
	$("#createModal").click(function(){
	    var target = $(this).attr('data-target');
	    var url = $(this).attr('href');
	    $(target).load(url);
	});
});

$(function(){
    //Custom Select
    $('select[name="sort"]').selectpicker({
        style: 'btn-primary', 
        menuStyle: 'dropdown-inverse',
        float:'none'
    });
    
    //Custom Select
    $("select[name='where']").selectpicker({
        style: 'btn-primary', 
        menuStyle: 'dropdown-inverse',
        float:'none'
    });
    
    //Custom Select
    $("select#Blog_type_id").selectpicker({
        style: 'btn-primary', 
        menuStyle: 'dropdown-inverse',
        float:'none'
    });
    
    //Custom Select
    $("select#Type_category").selectpicker({
        style: 'btn-primary', 
        menuStyle: 'dropdown-inverse',
        float:'none'
    });
    
    //Custom Select
    $("select#Link_category_id").selectpicker({
        style: 'btn-primary', 
        menuStyle: 'dropdown-inverse',
        float:'none'
    });
    
    //Custom Select
    $("select#File_category_id").selectpicker({
        style: 'btn-primary', 
        menuStyle: 'dropdown-inverse',
        float:'none'
    });
    
    //Custom Select
    $("select#User_sex").selectpicker({
        style: 'btn-primary', 
        menuStyle: 'dropdown-inverse',
        float:'none'
    });
    
    //Custom Select
    $("select#User_role_id").selectpicker({
        style: 'btn-primary', 
        menuStyle: 'dropdown-inverse',
        float:'none'
    });

    //Custom Select
    $("select#Ad_type_id").selectpicker({
        style: 'btn-primary', 
        menuStyle: 'dropdown-inverse',
        float:'none'
    });

    //Custom Select
    $("select.blog-date").selectpicker({
        style: 'btn-primary', 
        menuStyle: 'dropdown-inverse',
        float:'none'
    });

    //Custom Select
    $("select.blog-category").selectpicker({
        style: 'btn-primary', 
        menuStyle: 'dropdown-inverse',
        float:'none'
    });

    //Custom Select
    $("select#category-parent").selectpicker({
        style: 'btn-primary', 
        menuStyle: 'dropdown-inverse',
        float:'none'
    });
});
$(function(){
   //给分页的元素添加一定的class
   $('div.pagination > ul > li.previous > a').addClass('fui-arrow-left');
   $('div.pagination > ul > li.next > a').addClass('fui-arrow-right');
});
//顶部导航搜索提交
$(function(){
    //搜搜类型切换
    $('select[name=where]').change(function(){
        var obj = $(this);
        var where = obj.val();
        switch(where){
            case 'baidu':
                $('form#blog-search').attr('action','http://www.baidu.com/s');
                $('input#keyword').attr('name','word');
                $('input#keyword').attr('baiduSug','1');
                break;
//            case 'google':
//                $('form#blog-search').attr('action','http://www.google.com/search');
//                $('input#keyword').attr('name','as_q');
//                $('input#keyword').removeAttr('baiduSug');
//                break;
            case 'inner':
                $('form#blog-search').attr('action','/search');
                $('input#keyword').attr('name','keyword');
                $('input#keyword').removeAttr('baiduSug');
                break;
        }
    });
    $('button#search').click(function(){
        var obj = $(this).parent();
        var keywordObj = obj.find('input#keyword');
        var whereObj = obj.find('select[name=where]');
        var whereVal = whereObj.val();
        switch(whereVal){
            case 'baidu':
                if(keywordObj.val() != undefined && keywordObj.val() != ''){
                    keywordObj.attr('value','site:gowhich.com ' + keywordObj.val());
                }
                break;
            case 'google':
                
                break;
        }
//        return false;
        $('form#blog-search').submit();
    });
});
//点击下载
$(function(){
    $('a#download-btn').click(function(){
        var obj = $(this);
        var href = obj.attr('href');
        var id = obj.parent().find('input[name="download-id"]').val();
        $.ajax({
           url:'/resource/download',
           data:{id:id,url:href},
           dataType:'json',
           type:'post',
           success:function(data){
               if(data.code != undefined && data.code == 1){
                   window.open(href);
                   
                   return true;
               }else{
                   return false;
               }
           }
        });
        return false;
    });
});

//分类删除和锁定
$(function(){
   $('a#category-delete').click(function(){
      var obj = $(this);
      var id = obj.parent().find('input[name=category-id]').val();
      $.ajax({
         url:'/admin/category/delete',
         type:'post',
         data:{id:id},
         dataType:'json',
         success:function(data){
             if(data.code != undefined && data.code == 1){
                 if(data.status != undefined && data.status == 1){
                     obj.removeClass('btn-primary');
                 }
                 
             }
             
             if(data.code != undefined && data.code == 1){
                 if(data.status != undefined && data.status == 0){
                     obj.addClass('btn-primary');
                 }
             }
         }
      });
      return false;
   });
   
   $('a#category-lock').click(function(){
        var obj = $(this);
        var id = obj.parent().find('input[name=category-id]').val();
        $.ajax({
           url:'/admin/category/lock',
           type:'post',
           data:{id:id},
           dataType:'json',
           success:function(data){
                if(data.code != undefined && data.code == 1){
                    if(data.lock != undefined && data.lock == 1){
                        obj.removeClass('btn-primary');
                    }
                }

                if(data.code != undefined && data.code == 1){
                    if(data.lock != undefined && data.lock == 0){
                        obj.addClass('btn-primary');
                    }
                }
           }
        });
        return false;
   });
});

/**
 * 下拉菜单联动
 */
$(function(){
    $('ul.downlist-first-relation > li > a').toggle(function(){
        var obj = $(this);
        var child_obj = obj.parent().find('ul.downlist-second-relation');
        obj.addClass('current');
        child_obj.show(800);
//        child_obj.css({'display':'block'});
    },function(){
        var obj = $(this);
        var child_obj = obj.parent().find('ul.downlist-second-relation');
        obj.removeClass('current');
//        child_obj.css({'display':'none'});
        child_obj.hide(800);
    });
});

//更新分类
$(function(){
	$('button.update-category').click(function(){
		//获取内容的class
		var category_class = $(this).parent().find('#category-class').val();
		var id = $('.'+category_class).find('input[name=id]').val();
		var name = $('.'+category_class).find('input[name=name]').val();
		var alias = $('.'+category_class).find('input[name=alias]').val();
		$.ajax({
			url:'/admin/category/update/id/'+id,
			data:{'title':name,'alias':alias},
			type:'post',
			dataType:'json',
			success:function(data){
				if(data.status != undefined && data.status == 1)
				{
					$('#'+id).modal('hide');
					window.location.reload();
				}
				else
				{
					alert('更新失败，请重新提交');	
				}
			}
		});
	});
});
